package game
{
	import engine.core.*;
	import flash.events.*;
	import flash.net.*;
	import flash.system.*;
	import game.core.*;
	import game.messages.login.*;
	import game.network.*;
	import game.telemetry.*;
	import game.ui.controls.*;
	import game.ui.core.*;
	
	public class RxClientError
	{
		private static const s_defaultTitle:String = "System Error";
		private static const s_iconUrl_networkDown:String = "ui/portraits/system_message_network_down.png";
		private static const s_iconUrl_technicalDifficulties:String = "ui/portraits/system_message_technical_difficulties.png";
		private static const s_iconUrl_maintenance:String = "ui/portraits/system_message_were_working_on_it.png";
		private static const s_iconUrl_sorry:String = "ui/portraits/system_message_sorry.png";
		private static const s_buttonText_refresh:String = "Refresh";
		private static var s_loginAlert:Boolean = false;
		private static var s_gamePresentedToUser:Boolean = false;
		private static var s_alertShown:Boolean;
		
		public function RxClientError()
		{
		}
		
		public static function OnLoginError(param1:int) : void
		{
			var _loc_3:String = null;
			var _loc_4:String = null;
			var _loc_8:Object = null;
			var _loc_9:uint = 0;
			var _loc_2:* = s_defaultTitle;
			var _loc_5:Array = [s_buttonText_refresh];
			var _loc_6:int = -1;
			var _loc_7:String = "client_login_error";
			switch(param1)
			{
				case RxLogin_ConnectStatusMessage.STATUS_LOGIN_FAILED_INCOMPATIBLE_VERSION:
				{
					_loc_3 = "Your client seems to be out of date.  Please update and relaunch.";
					_loc_4 = s_iconUrl_networkDown;
					break;
				}
				case RxTransactionServer.STATUS_LOGOUT_DISCONNECTED:
				{
					_loc_3 = "The connection to the game server was lost.  Please refresh the browser.";
					_loc_4 = s_iconUrl_networkDown;
					break;
				}
				case RxLogin_LoginStatusMessage.STATUS_LOGIN_FAILED_GENERAL_ERROR:
				default:
				{
					_loc_3 = "A general error occured on the server while attempting to log in. Refresh the browser to try again.";
					_loc_4 = s_iconUrl_technicalDifficulties;
					break;
				}
				case RxLogin_LoginStatusMessage.STATUS_LOGIN_FAILED_SERVICE_UNAVAILABLE:
				{
					_loc_3 = "This user logged in from a different location.";
					_loc_4 = s_iconUrl_technicalDifficulties;
					break;
				}
				case RxLogin_LoginStatusMessage.STATUS_LOGIN_FAILED_USER_MAINTENANCE:
				{
					_loc_3 = "The game is temporarily unavailable. Please try again later.";
					_loc_4 = s_iconUrl_maintenance;
					break;
				}
				case RxLogin_LoginStatusMessage.STATUS_LOGIN_FAILED_SOFTWARE_RENDERING:
				{
					_loc_3 = "This user is undergoing maintenance and is temporarily unavailable. Please try again later.";
					_loc_4 = s_iconUrl_maintenance;
					break;
				}
				case RxLogin_LoginStatusMessage.STATUS_LOGIN_FAILED_SOFTWARE_RENDERING:
				{
					_loc_7 = "client_software_rendering";
					_loc_8 = RxSystemInfo.Collect(RxStage.stage);
					_loc_9 = _loc_8.graphicsHardwareAcceleration;
					switch(_loc_9)
					{
						case RxSystemInfo.HW_DISABLED_USER_DISABLED:
						{
							_loc_2 = "Change Flash Settings";
							_loc_3 = "Hardware acceleration has been disabled in the Flash Player settings.  Please enable hardware acceleration and refresh your browser.\n";
							_loc_4 = s_iconUrl_technicalDifficulties;
							_loc_5 = ["Change Settings", "Software Mode"];
							_loc_6 = 0;
							break;
						}
						case RxSystemInfo.HW_DISABLED_OLD_DRIVER:
						{
							_loc_2 = "Update Graphics Drivers";
							_loc_3 = "Flash Player indicates that your graphics hardware drivers need to be updated to run in accelerated hardware mode.\n" + "\n" + "Please <font color=\'#0000FF\'><a href=\'http://support.rumblegames.com/link/portal/16106/16138/Article/49/Error-received-Flash-Player-indicates-that-your-graphics-hardware-drivers-need-to-be-updated\'>" + "visit this Rumble Support page</a></font> for information on upgrading your drivers.\n";
							_loc_4 = s_iconUrl_technicalDifficulties;
							_loc_5 = ["Software Mode"];
							break;
						}
						case RxSystemInfo.HW_DISABLED_EXPLICIT:
						case RxSystemInfo.HW_DISABLED_UNAVAILABLE:
						{
							_loc_2 = "Unsupported Hardware";
							_loc_3 = "Flash Player doesn\'t support the graphics hardware in your system.\n" + "\n" + "Please <font color=\'#0000FF\'><a href=\'http://support.rumblegames.com/link/portal/16106/16138/Article/48/Error-received-Flash-Player-does-not-support-the-graphics-hardware-in-your-system\'>" + "visit this Rumble Support page</a></font> for more information.\n";
							_loc_4 = s_iconUrl_sorry;
							_loc_5 = ["Software Mode"];
							break;
						}
						case RxSystemInfo.HW_DISABLED_UNKNOWN_CAUSE:
						default:
						{
							_loc_2 = "Update Flash Player";
							_loc_3 = "This version of the Flash Player doesn\'t support your graphics hardware.  Please try updating to the latest Flash Player.\n" + "\n" + "<font color=\'#0000FF\'><a href=\'http://support.rumblegames.com/link/portal/16106/16138/Article/46/Error-received-Flash-Player-cannot-locate-a-supported-graphics-hardware-on-your-system\'>" + "Visit this Rumble Support page</a></font> for information on updating your Flash Player.\n";
							_loc_4 = s_iconUrl_technicalDifficulties;
							_loc_5 = ["Software Mode"];
							break;
						}
					}
					break;
				}
			}
			ShowErrorAlert(_loc_7, _loc_2, _loc_3, _loc_4, _loc_5, _loc_6);

		}
		
		public static function OnFakeLogin() : void
		{
			ShowAlert(s_defaultTitle, "There was a problem communicating with the platform.  You have been logged in with a temporary account.", s_iconUrl_sorry, ["OK"]);

		}
		
		public static function OnFailedToConnectToServer() : void
		{
			ShowErrorAlert("client_connect_failed", s_defaultTitle, "Failed to connect to the game server.  Refresh the browser to try again.", s_iconUrl_networkDown, [s_buttonText_refresh]);

		}
		
		public static function OnLoginTimeout() : void
		{
			ShowErrorAlert("client_login_timeout", s_defaultTitle, "Login request timed out.  Please refresh the browser.", s_iconUrl_networkDown, [s_buttonText_refresh]);

		}
		
		public static function OnLoadTimeout() : void
		{
			ShowErrorAlert("client_load_timeout", s_defaultTitle, "The game failed to load in a timely manner.  Please refresh the browser.", s_iconUrl_networkDown, [s_buttonText_refresh]);

		}
		
		public static function OnUncaughtError(event:UncaughtErrorEvent) : void
		{
			var _loc_2:String = null;
			var _loc_3:String = null;
			var _loc_4:Error = null;
			var _loc_5:ErrorEvent = null;
			if (event.error is Error)
			{
				_loc_4 = event.error as Error;
				_loc_2 = "Uncaught Exception [" + _loc_4.errorID + "] : " + _loc_4.name + " : " + _loc_4.message;
				_loc_3 = _loc_4.getStackTrace();
				_loc_3 = RxCallstack.ReformatStack(_loc_3);
			}
			else if (event.error is ErrorEvent)
			{
				_loc_5 = event.error as ErrorEvent;
				_loc_2 = "Uncaught Exception [" + _loc_5.errorID + "] : " + _loc_5.text;
				_loc_3 = null;
			}
			else
			{
				_loc_2 = "Uncaught Exception on " + event.error.toString();
				_loc_3 = null;
			}
			RxLog.Error(_loc_2, _loc_3);
			if (_loc_3 != null)
			{
				_loc_2 = "<font size=\'10\'>" + _loc_2 + "\n" + _loc_3 + "</font>";
			}
			_loc_2 = "A general error occured on the client. Refresh the browser to try again.\n\n" + _loc_2;
			ShowErrorAlert("client_error", "Client Error", _loc_2, s_iconUrl_technicalDifficulties, [s_buttonText_refresh]);

		}
		
		public static function OnGameShownToUser() : void
		{
			s_gamePresentedToUser = true;

		}
		
		public static function HasGameBeenShownToUser() : Boolean
		{
			return s_gamePresentedToUser;
		}
		
		private static function ShowErrorAlert(param1:String, param2:String, param3:String, param4:String, param5:Array = null, param6:int = -1) : void
		{
			if (s_alertShown)
			{
	
			}
			s_alertShown = true;
			RxMain.instance.StopUpdate();
			if (s_gamePresentedToUser)
			{
			}
			else
			{
				RxTelemetry.GameNotLoaded(param1);
			}
			if (RxTransactionServer.instance != null)
			{
				RxTransactionServer.instance.Disconnect();
			}
			ShowAlert(param2, param3, param4, param5, param6);

		}
		
		private static function ShowAlert(param1:String, param2:String, param3:String, param4:Array = null, param5:int = -1) : void
		{
			if (s_loginAlert)
			{
	
			}
			s_loginAlert = true;
			var _loc_6:* = param2.replace(/<[^>]*>""<[^>]*>/g, "");
			RxLog.Warning("client alert: " + _loc_6);
			if (param4 != null && param5 == -1)
			{
				param5 = param4.length - 1;
			}
			var _loc_7:* = RxDialogManager.instance.ShowAlert(param1, param2, param4, param5, param3, null, false, true, true);
			RxDialogManager.instance.ShowAlert(param1, param2, param4, param5, param3, null, false, true, true).SetMinHeight(350);
			_loc_7.SetCallback(OnAlertClosed);

		}
		
		private static function OnAlertClosed(param1:RxAlert, param2:String) : void
		{
			var _loc_3:SharedObject = null;
			s_loginAlert = false;
			if (param2 == s_buttonText_refresh)
			{
				RxExternalInterface.RefreshBrowser();
			}
			else if (param2 == "Change Settings")
			{
				Security.showSettings(SecurityPanel.DISPLAY);
				ShowAlert("Refresh Browser", "Please refresh the browser for the settings changes to take effect.", s_iconUrl_technicalDifficulties, [s_buttonText_refresh]);
			}
			else if (param2 == "Software Mode")
			{
				_loc_3 = RxLocalSettings.GetObject();
				if (_loc_3)
				{
					_loc_3.data.allowSoftwareMode = 1;
					RxLocalSettings.WriteSettings();
				}
				RxExternalInterface.RefreshBrowser();
			}

		}
	}
}